<?php

/**
 * @Author: kendy
 * @Date:    2020-04-15 16:21:01
 * @Last Modified by:   jiayanting
 * @Last Modified time: 2020-04-16 17:13:26
 */
namespace app\api\job;
use think\queue\Job;
use think\Log;
use think\Db;
use think\loader;  
use app\common\model\LogOrderModel as lo;
class Cancelorder{
	public function fire(Job $job,$data){
		//如有必要，可以根据业务去求和数据库中的最新数据，判断该任务是否仍有必要执行
		$isJobStillNeedToBeDone = $this->checkDatabaseToSeeIfJobNeedToBeDone($data);

		if(!$isJobStillNeedToBeDone){
              $job->delete();
              return;
		}
		$isJobDone = $this->doNoPaymentJob($data);
		if($isJobDone){
			//如果任务成功，记得删除任务
			$job->delete();
		}else{
			if($job->attempts() > 3){
				//通过这个方法可以检查这个任务已经重试几次了
				$job->delete();
			}
		}
	}
    /**
     * 有些消息在到达消费者时，可能已经不在需要执行了
     * [checkDatabaseToSeeIfJobNeedToBeDone description]
     * @Author   kendy
     * @DateTime 2020-03-26T09:03:20+0800
     * @param    [type]                   $data [发布任务时自定义的数据]
     * @return   [type]                         [任何执行的结果]
     */
	public function checkDatabaseToSeeIfJobNeedToBeDone($data){
		return true;
	}

    private function doNoPaymentJob($data){ 
     //酒店取消订单  
      if($data['type'] == 1){ 
      $hotelOrderInfo = Db::name('hotel_order')
      ->alias("ho")
      ->join("hotel_room hr",'hr.room_id = ho.room_id')
      ->where(['ho.order_id'=>$data['order_id'],'order_status'=>0,'ho.is_del'=>0])
      ->find();  
       if(!empty($hotelOrderInfo)){
       //减少该酒店房间的库存
        if($hotelOrderInfo['room_count'] > 0){

          Db::startTrans();
          try{
            //修改订单的状态
            Db::name("hotel_order")
            ->where("order_id",$hotelOrderInfo['order_id'])
            ->update(['order_status'=>5]);
            //修改房间的库存
            Db::name("hotel_room")
            ->where("room_id",$hotelOrderInfo['room_id'])
            ->setDec('room_count');
            //创建订单记录 
            $logOrder = new lo();//订单类型 1酒店，2景点，3跟团，4捡漏
            $logOrder->addLogOrder($data['order_id'],1,$hotelOrderInfo['uid'],0,'订单长时间未支付，系统自动取消订单'); 
             //给用户发送消息提示 1景点订单 2酒店订单 3旅行社订单
            send_user_message(9, 2, $hotelOrderInfo['uid'],$hotelOrderInfo['order_sn'],$hotelOrderInfo['room_name'],$hotelOrderInfo['order_id'],2);
            Db::commit();
            return true;
          }catch(\Exception $e){
            Db::rollback();
            return false; 
          }
          return false; 
        }
 
        }
       } 
       if($data['type'] == 2){
       //景点取消订单
        $tourOrderInfo = Db::name("tour_order")
                        ->alias("to")
                        ->join("tour_ticket tt",'tt.tid = to.type_id','left')
                        ->where(['to.is_del'=>0,'to.order_id'=>$data['order_id'],'order_status'=>0])
                        ->find();
       if(!empty($tourOrderInfo)){ 
          try{
             Db::startTrans();
             //修改订单的状态，
             Db::name('tour_order')
             ->where("order_id",$data['order_id'])
             ->update(['order_status'=>5]);
             //创建订单记录，
             $logOrder = new lo();//订单类型 1酒店，2景点，3跟团，4捡漏
             $logOrder->addLogOrder($data['order_id'],2,$tourOrderInfo['user_id'],0,'订单长时间未支付，系统自动取消订单'); 
             //给用户发送消息提示1景点订单 2酒店订单 3旅行社订单 
             send_user_message(9, 2, $tourOrderInfo['user_id'],$tourOrderInfo['order_sn'],'',$tourOrderInfo['order_id'],1);
           Db::commit(); 
           return true;
          }catch(Exception $e){
            Db::rollback(); 
            return false;
          }
          return false;
         } 
       }
      if($data['type'] == 3){
      //跟团游取消订单
      $gtourOrderInfo = Db::name('gtour_order')
                        ->alias("go")
                        ->join("group_tour gt",'gt.id = go.gtour_id','left')
                        ->where(['go.is_del'=>0,'go.order_id'=>$data['order_id'],'order_status'=>0])
                        ->find();
      if(!empty($gtourOrderInfo)){
          try{
            Db::startTrans();
           //修改订单的状态，
          Db::name('gtour_order')
          ->where("order_id",$data['order_id'])
          ->update(['order_status'=>5]);
           //创建订单记录，
            $logOrder = new lo();//订单类型 1酒店，2景点，3跟团，4捡漏
            $logOrder->addLogOrder($data['order_id'],3,$gtourOrderInfo['user_id'],0,'订单长时间未支付，系统自动取消订单'); 
           //给用户发送消息提示
             send_user_message(9, 2, $gtourOrderInfo['user_id'],$gtourOrderInfo['order_sn'],'',$gtourOrderInfo['order_id'],1);

            Db::commit(); 
            return true;
          }catch(Exception $e){ 
            Db::rollback(); 
            return false;
          }
          return false;
        }
       } 

  }

}